package com.ly.heart.service.impl;

import java.util.List;
import java.util.Optional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.utils.DateUtils;
import com.ly.heart.domain.ActTaskCommentEntity;
import com.ly.heart.domain.KingSaleRefundDetailEntity;
import com.ly.heart.domain.dto.KingSaleOutInventoryMaterialQueryDto;
import com.ly.heart.domain.dto.KingSaleRefundDetailDto;
import com.ly.common.core.domain.ActCurrentTaskVo;
import com.ly.heart.domain.vo.KingSaleRefundDetailVo;
import com.ly.heart.mapper.KingSaleRefundDetailMapper;
import com.ly.heart.service.IActModelService;
import com.ly.heart.service.IActTaskCommentService;
import com.ly.heart.service.IKingSaleRefundDetailService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 金蝶-销售退货单明细Service业务层处理
 * @author wutao
 * @date 2024-07-14
 */
@Service
public class KingSaleRefundDetailServiceImpl extends ServiceImpl<KingSaleRefundDetailMapper, KingSaleRefundDetailEntity> implements IKingSaleRefundDetailService {

    @Resource
    private KingSaleRefundDetailMapper kingSaleRefundDetailEntityMapper;
    @Resource
    private IActModelService iActModelService;
    @Resource
    private IActTaskCommentService actTaskCommentService;

    /**
     * 查询金蝶-销售退货单明细
     * @param id 金蝶-销售退货单明细ID
     * @return 金蝶-销售退货单明细
     */
    @Override
    public KingSaleRefundDetailEntity selectKingSaleRefundDetailEntityById (String id) {
        return kingSaleRefundDetailEntityMapper.selectKingSaleRefundDetailEntityById ( id );
    }

    /**
     * 查询金蝶-销售退货单明细列表
     * @param kingSaleRefundDetailEntity 金蝶-销售退货单明细
     * @return 金蝶-销售退货单明细
     */
    @Override
    public List<KingSaleRefundDetailEntity> selectKingSaleRefundDetailEntityList (KingSaleRefundDetailEntity kingSaleRefundDetailEntity) {
        return kingSaleRefundDetailEntityMapper.selectKingSaleRefundDetailEntityList ( kingSaleRefundDetailEntity );
    }

    /**
     * 新增金蝶-销售退货单明细
     * @param kingSaleRefundDetailEntity 金蝶-销售退货单明细
     * @return 结果
     */
    @Override
    public int insertKingSaleRefundDetailEntity (KingSaleRefundDetailEntity kingSaleRefundDetailEntity) {
        kingSaleRefundDetailEntity.setCreateTime ( DateUtils.getNowDate () );
        return kingSaleRefundDetailEntityMapper.insertKingSaleRefundDetailEntity ( kingSaleRefundDetailEntity );
    }

    /**
     * 修改金蝶-销售退货单明细
     * @param kingSaleRefundDetailEntity 金蝶-销售退货单明细
     * @return 结果
     */
    @Override
    public int updateKingSaleRefundDetailEntity (KingSaleRefundDetailEntity kingSaleRefundDetailEntity) {
        kingSaleRefundDetailEntity.setUpdateTime ( DateUtils.getNowDate () );
        return kingSaleRefundDetailEntityMapper.updateKingSaleRefundDetailEntity ( kingSaleRefundDetailEntity );
    }


    /**
     * 查询销售退货与销售退货明细关联关系记录
     * @param kingSaleRefundDetailEntity
     * @return
     */
    @Override
    public List<KingSaleRefundDetailEntity> queryRelationList (KingSaleRefundDetailEntity kingSaleRefundDetailEntity) {
        List<KingSaleRefundDetailEntity> kingSaleRefundDetailEntities = kingSaleRefundDetailEntityMapper.queryRelationList ( kingSaleRefundDetailEntity );
        return kingSaleRefundDetailEntities;
    }

    /**
     * 批量删除金蝶-销售退货单明细
     * @param ids 需要删除的金蝶-销售退货单明细ID
     * @return 结果
     */
    @Override
    public int deleteKingSaleRefundDetailEntityByIds (String[] ids) {
        return kingSaleRefundDetailEntityMapper.deleteKingSaleRefundDetailEntityByIds ( ids );
    }

    /**
     * 删除金蝶-销售退货单明细信息
     * @param id 金蝶-销售退货单明细ID
     * @return 结果
     */
    @Override
    public int deleteKingSaleRefundDetailEntityById (String id) {
        return kingSaleRefundDetailEntityMapper.deleteKingSaleRefundDetailEntityById ( id );
    }

    @Override
    public KingSaleRefundDetailEntity getKingSaleRefundTotalNumAndTaxPrice(KingSaleOutInventoryMaterialQueryDto queryDto) {
        return kingSaleRefundDetailEntityMapper.getKingSaleRefundTotalNumAndTaxPrice(queryDto);
    }

    @Override
    public IPage<KingSaleRefundDetailVo> selectKingSaleRefundDetailVoPage(KingSaleRefundDetailDto kingSaleRefundDetailDto) {
        IPage<KingSaleRefundDetailVo> kingSaleRefundDetailVoIPage = kingSaleRefundDetailEntityMapper.selectKingSaleRefundDetailVoPage ( kingSaleRefundDetailDto );
        kingSaleRefundDetailVoIPage.getRecords ().forEach ( s -> {
            //审批人信息
            ActTaskCommentEntity actTaskComment = actTaskCommentService.queryLastComment ( s.getInstanceId () );
            Optional.ofNullable ( actTaskComment ).ifPresent ( taskComment -> {
                s.setApprovedUser ( taskComment.getApprovedUser () );
                s.setRemark ( taskComment.getRemark () );
            } );
            ActCurrentTaskVo currentTaskVo = iActModelService.getCurrentTaskByInstanceId ( s.getInstanceId () );
            Optional.ofNullable ( currentTaskVo ).ifPresent ( currentTask -> {
                s.setCurrentTask ( currentTask );
            } );
        } );
        return kingSaleRefundDetailVoIPage;
    }

    @Override
    public IPage<KingSaleRefundDetailVo> menuSaleRefundDetailList(KingSaleRefundDetailDto kingSaleRefundDetailDto) {
        return kingSaleRefundDetailEntityMapper.menuSaleRefundDetailList(kingSaleRefundDetailDto);
    }
}
